package com.lidroid.xutils_sample.slice;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.table.Table;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils_sample.bean.User;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class DbUtilsPageSliceTest {

    private static final String name = "DbUtils";
    private int id = 1;
    private static final String email = "123@456.com";
    private final Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
    private final AbilitySlice abilitySlice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

    private DbUtils db;
    private User user;

    @Before
    public void setUp() throws Exception {
        try {
            Table.get(User.class);
        } catch (Exception e) {
            Table.get(User.class);
        }

        db = DbUtils.create(abilitySlice);
        user = new User();
    }

    // 增
    @Test
    public void btnSetClick() throws DbException {
        user.setName(name);

        List<User> all = db.findAll(User.class);
        assertEquals(0, all.size());

        db.saveBindingId(user);

        all = db.findAll(User.class);
        assertEquals(1, all.size());
    }

    // 查
    @Test
    public void btnGetClick() throws DbException {
        String getEmail;
        String getName;
        int getId;

        user.setId(id);
        user.setName(name);
        user.setEmail(email);
        db.saveBindingId(user);

        List<User> all = db.findAll(User.class);

        if (all.size() == 1) {
            getEmail = all.get(0).getEmail();
            getName = all.get(0).getName();
            getId = all.get(0).getId();
        } else {
            getEmail = all.get(1).getEmail();
            getName = all.get(1).getName();
            getId = all.get(1).getId();
            id = 3;
        }

        assertEquals(getEmail, email);
        assertEquals(getName, name);
        assertEquals(getId, id);
    }

    // 删
    @Test
    public void btnDelClick() throws DbException {
        user.setId(id);
        user.setName(name);
        user.setEmail(email);
        db.saveBindingId(user);

        List<User> all = db.findAll(User.class);
        assertEquals(1, all.size());

        db.deleteById(User.class,id);
        all = db.findAll(User.class);
        assertEquals(0, all.size());
    }

    // 改
    @Test
    public void btnUpdateClick() throws DbException {
        String updateName;

        user.setId(id);
        user.setName(name);
        user.setEmail(email);
        db.saveBindingId(user);

        List<User> all = db.findAll(User.class);
        updateName = all.get(0).getName();
        assertEquals(name, updateName);

        user.setName("update-name");
        db.update(user);
        all = db.findAll(User.class);
        updateName = all.get(2).getName();
        assertEquals("update-name", updateName);
    }
}